import streamlit as st
import streamlit_antd_components as sac
from data_utils import load_calculators, update_calculator, format_calc_id
from audit import log_calc_audit
import math

def status_management():
    """QA管理页面"""
    st.subheader("计算器状态管理")

    # 加载所有计算器
    all_calculators = load_calculators()

    # 状态统计
    status_counts = {
        "draft": len([c for c in all_calculators if c.status == "draft"]),
        "active": len([c for c in all_calculators if c.status == "active"]),
        "inactive": len([c for c in all_calculators if c.status == "inactive"])
    }

    # 显示状态统计
    col1, col2, col3 = st.columns(3)
    with col1:
        st.metric("草稿", status_counts["draft"])
    with col2:
        st.metric("已激活", status_counts["active"])
    with col3:
        st.metric("已停用", status_counts["inactive"])

    # 筛选条件
    col1, col2 = st.columns(2)
    with col1:
        name_filter = st.text_input("按名称筛选", key="name_filter")
    with col2:
        status_filter = st.selectbox(
            "按状态筛选",
            ["全部", "draft", "active", "inactive"],
            key="status_filter"
        )

    # 应用筛选
    filtered_calculators = all_calculators
    if name_filter:
        filtered_calculators = [
            c for c in filtered_calculators
            if name_filter.lower() in c.name.lower()
        ]
    if status_filter != "全部":
        filtered_calculators = [
            c for c in filtered_calculators
            if c.status == status_filter
        ]

    # 分页设置
    items_per_page = 10
    total_items = len(filtered_calculators)

    # 获取当前页码
    current_page = sac.pagination(
        total=total_items,
        page_size=items_per_page,
        align='center',
        show_total=True,
        previous="上一页",
        next="下一页",
        key="status_pagination"
    )

    start_idx = (current_page - 1) * items_per_page
    end_idx = min(start_idx + items_per_page, total_items)
    page_calculators = filtered_calculators[start_idx:end_idx]

    # 显示计算器列表
    if not page_calculators:
        st.info("没有找到符合条件的计算器")
    else:
        for calc in page_calculators:
            with st.expander(f"{calc.name} (当前状态: {calc.status})"):
                new_status = st.selectbox(
                    "设置状态",
                    ["draft", "active", "inactive"],
                    index=["draft", "active", "inactive"].index(calc.status),
                    key=f"status_{calc.id}"
                )
                if st.button("更新状态", key=f"update_{calc.id}"):
                    old_status = calc.status
                    update_calculator(calc.id, {"status": new_status})
                    log_calc_audit(
                        calc_id=format_calc_id(calc.id),
                        action="update_status",
                        username=st.session_state.username,
                        details=f"状态从 {old_status} 更新为 {new_status}",
                        old_value=old_status,
                        new_value=new_status
                    )
                    st.toast("状态已更新")
                    st.rerun()
